package com.qiangesoft.im.core;

import com.qiangesoft.im.entity.SysUser;
import com.qiangesoft.im.pojo.bo.ImMessageBO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 消息处理器
 *
 * @author qiangesoft
 * @date 2023-08-30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MessageHandlerService {

    private final ISysUserService sysUserService;

    /**
     * 构建消息
     *
     * @param messageBO
     * @return
     */
    public ImMessageVO buildVo(ImMessageBO messageBO) {
        ImMessageVO messageVO = new ImMessageVO();
        Long messageId = messageBO.getId();
        if (messageId == null) {
            return messageVO;
        }
        messageVO.setId(messageBO.getId());
        messageVO.setChatId(messageBO.getChatId());
        messageVO.setMessageType(messageBO.getMessageType());
        messageVO.setMessage(messageBO.getMessage());
        messageVO.setReadFlag(false);
        messageVO.setSendTime(messageBO.getSendTime());
        SysUser sysUser = sysUserService.getById(messageBO.getSenderId());
        SysUserVo sysUserVo = SysUserVo.fromEntity(sysUser);
        messageVO.setSender(sysUserVo);
        return messageVO;
    }
}
